conf = require("../config")
if not conf.blink_cmp.enabled then return {} end

return {
  {
    "folke/lazydev.nvim",
    ft = "lua", -- only load on lua files
    opts = {
      library = {
        -- See the configuration section for more details
        -- Load luvit types when the `vim.uv` word is found
        { path = "luvit-meta/library", words = { "vim%.uv" } },
      },
    },
  },
  -- { "Bilal2453/luvit-meta", lazy = true }, -- optional `vim.uv` typings
  {
    'saghen/blink.cmp',
    -- optional: provides snippets for the snippet source
    dependencies = {
      'rafamadriz/friendly-snippets',
      { 'xzbdmw/colorful-menu.nvim', opts = {} },
    },
    -- event = { 'BufReadPost', 'BufNewFile' },
    event = { 'InsertEnter', 'CmdlineEnter' },

    -- use a release tag to download pre-built binaries
    version = '*',
    -- AND/OR build from source, requires nightly: https://rust-lang.github.io/rustup/concepts/channels.html#working-with-nightly-rust
    -- build = 'cargo build --release',
    -- If you use nix, you can build from source using latest nightly rust with:
    -- build = 'nix run .#build-plugin',

    ---@module 'blink.cmp'
    ---@type blink.cmp.Config
    opts = {
      -- 'default' for mappings similar to built-in completion
      -- 'super-tab' for mappings similar to vscode (tab to accept, arrow keys to navigate)
      -- 'enter' for mappings similar to 'super-tab' but with 'enter' to accept
      -- See the full "keymap" documentation for information on defining your own keymap.
      keymap = {
        -- set to 'none' to disable the 'default' preset
        preset = 'none',
        ['<C-c>'] = { 'show', 'hide' },
        ['<C-d>'] = { 'show_documentation', 'hide_documentation' },
        -- Use <C-j/k> to switch in items
        ['<C-k>'] = { 'select_prev', 'fallback' },
        ['<C-j>'] = { 'select_next', 'fallback' },
        -- Use <C-b/f> to scroll the docs
        ['<C-b>'] = { 'scroll_documentation_up', 'fallback' },
        ['<C-f>'] = { 'scroll_documentation_down', 'fallback' },
        ['<CR>']  = { 'accept', 'fallback' },
        ['<Tab>'] = {
          function(cmp)
            if cmp.snippet_active() then return cmp.accept()
            else return cmp.select_and_accept() end
          end,
          'snippet_forward',
          'fallback'
        },
        ['<S-Tab>'] = { 'snippet_backward', 'fallback' },
      },

      appearance = {
        -- Sets the fallback highlight groups to nvim-cmp's highlight groups
        -- Useful for when your theme doesn't support blink.cmp
        -- Will be removed in a future release
        use_nvim_cmp_as_default = true,
        -- Set to 'mono' for 'Nerd Font Mono' or 'normal' for 'Nerd Font'
        -- Adjusts spacing to ensure icons are aligned
        nerd_font_variant = 'mono'
      },

      -- Default list of enabled providers defined so that you can extend it
      -- elsewhere in your config, without redefining it, due to `opts_extend`
      sources = {
        default = { 'lsp', 'path', 'snippets', 'buffer' },
        providers = {
          lazydev = {
            name = "LazyDev",
            module = "lazydev.integrations.blink",
            score_offset = 100,
          },
        },
      },

      cmdline = {
        enabled = true,
        completion = {
          menu = {
            auto_show = true
          },
          ghost_text = { enabled = true },
        },
        keymap = {
          -- set to 'none' to disable the 'default' preset
          preset = 'none',
          ['<C-c>'] = { 'show', 'hide' },
          -- Use <C-m/n> to switch in items
          ['<C-k>'] = { 'select_prev', 'fallback' },
          ['<C-j>'] = { 'select_next', 'fallback' },
          -- Use <C-b/f> to scroll the docs
          ['<C-b>'] = { 'scroll_documentation_up', 'fallback' },
          ['<C-f>'] = { 'scroll_documentation_down', 'fallback' },
          ['<CR>'] = { 'accept_and_enter', 'fallback' },
          ['<Tab>'] = { 'select_next', 'fallback' },
          ['<S-Tab>'] = { 'select_prev', 'fallback' },
        },
      },

      completion = {
        keyword = { range = 'full'},

        accept = { auto_brackets = { enabled = true }, },

        list = {
          selection = {
            preselect = function(ctx)
              return ctx.mode ~= 'cmdline' and not require('blink.cmp').snippet_active({ direction = 1 })
            end,
            auto_insert = true,
          }
        },

        menu = {
          border = 'rounded',
          draw = {
            columns = {
              -- { "kind_icon", "kind" },
              { "kind_icon" },
              { "label", "label_description", "kind", gap = 1 }
            },
            treesitter = { 'lsp' },
            components = {
              label = {
                text = function(ctx)
                  return require("colorful-menu").blink_components_text(ctx)
                end,
                highlight = function(ctx)
                  return require("colorful-menu").blink_components_highlight(ctx)
                end,
              },
            },
          },
        },
        -- Show documentation when selecting a completion item
        documentation = {
          window = { border = 'rounded' },
          auto_show = true,
          auto_show_delay_ms = 200
        },

        -- Display a preview of the selected item on the current line
        ghost_text = { enabled = true },

      },
      -- Experimental signature help support
      signature = {
        enabled = true,
        window = {border = 'single'},
      },
    },
    opts_extend = { "sources.default" },
  }
}
